package com.example.service.impl;

import com.example.dao.BookMapper;
import com.example.domain.Book;
import com.example.domain.Record;
import com.example.domain.User;
import com.example.entity.PageResult;
import com.example.service.BookService;
import com.example.service.RecordService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private RecordService recordService;

    @Override
    public PageResult selectNewBooks(Integer pageNum, Integer pageSize) {

        //  开启分页
        PageHelper.startPage(pageNum, pageSize);

        //  调用 Mapper 查询新书
        Page<Book> books = bookMapper.selectNewBooks();

        //  封装返回结果
        return new PageResult(books.getTotal(), books.getResult());
    }

    @Override
    public Book findBookById(String id) {
        return bookMapper.findBookById(id);
    }
    /*    黑马原名  findById
        Book findById(String id);
    */

    @Override
    public Integer borrowBook(Book book) {

        // 设置借阅状态
        book.setStatus("1");

        // 借阅时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String borrowTime = format.format(new Date());
        book.setBorrowTime(borrowTime);

        // 调用 mapper
        return bookMapper.editBook(book);

    }

    @Override
    public PageResult search(Book book, int pageNum, int pageSize) {

        //  开启分页
        PageHelper.startPage(pageNum, pageSize);

        //  调用 Mapper 查询
        Page<Book> books = bookMapper.searchBooks(book);

        //  封装返回结果
        return new PageResult(books.getTotal(), books.getResult());

    }

    @Override
    public Integer addBook(Book book) {

        // 设置上架时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String uploadTime = format.format(new Date());
        book.setUploadTime(uploadTime);

        return bookMapper.addBook(book);
    }

    @Override
    public Integer editBook(Book book) {
        return bookMapper.editBook(book);
    }

    @Override
    public PageResult searchBorrowed(Book book, User user, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        book.setBorrower(user.getName());
        Page<Book> bookPage = null;

        if (user.getRole().equals("ADMIN")) {
            bookPage = bookMapper.selectBorrowed(book);
        } else {
            bookPage = bookMapper.selectMyBorrowed(book);
        }

        return new PageResult(bookPage.getTotal(), bookPage.getResult());
    }

    @Override
    public Boolean returnBook(String id, User user) {

        Book book = bookMapper.findBookById(id);

        Boolean flag = book.getBorrower().equals(user.getName());

        Integer num = 0;
        if (flag) {
            book.setStatus("2");
            num = bookMapper.editBook(book);
        }

        return num > 0;
    }

    @Override
    public Boolean confirmed(String id) {

        Book book = bookMapper.findBookById(id);

        String bookBorrower = book.getBorrower();
        String bookBorrowTime = book.getBorrowTime();

        book.setStatus("0");
        book.setBorrower("");
        book.setBorrowTime("");
        book.setReturnTime("");

        Integer num = bookMapper.editBook(book);

        if (num > 0) {
            // 归还无误，新增记录
            Record record = new Record();
            // 记录信息
            record.setBookisbn(book.getIsbn());
            record.setBookname(book.getName());
            record.setBorrower(bookBorrower);
            record.setBorrowTime(bookBorrowTime);

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String remandTime = format.format(new Date());
            record.setRemandTime(remandTime);

            recordService.addRecord(record);
        }

        return num > 0;
    }


}
/*


 */